package study.job.spring.support.scheduling;

import cn.hutool.core.thread.NamedThreadFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;

/**
 * 自定义调度线程池
 * （1）继承 SchedulingConfigurer，通过 setScheduler 方法注入自定义 ScheduledExecutorService
 */
@Slf4j
//@Configuration
public class _3_SchedulingConfig implements SchedulingConfigurer {

    @Override
    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
        taskRegistrar.setScheduler(customScheduler());
    }

    @Bean(destroyMethod = "shutdown")
    public ExecutorService customScheduler() {
        ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(100, new NamedThreadFactory("test-job", false));
        return executor;
    }
}
